/*============================================================================*/

* Do: Regressions Part 2 - ENAHO
* Last update: 09.05.2023 

/*============================================================================*/

* SECTION: FOLDERS
* ==============================================================================

*------------------------ SUBFOLDERS (DO NOT CHANGE)
gl in "$root/Final"
gl analysis "$root/Analysis"

* ==============================================================================
* SECTION: REGRESSIONS
* ==============================================================================
cap log close
log using "regressions_part2_GT", replace

use "$in/data_regressions_enaho", clear

recode occupation 0=., gen(occ1)
replace occ1 = 12 - occ1

xtset, clear

* Outcomes

local outcome problem_emp problem_corrupt log_crime_violent log_crime_nonviolent problem_crime employment_status formalemploy occ1 lnwage1 lnhhinc lnhhexp 

local outemp employment_status formalemploy occ1 lnwage1 lnhhinc lnhhexp 

* Covariates

local indvchar c.age##c.age ib1.education_group i.marital_status hhsize  

local work formalemploy i.occupation

local vztype share_combined_std 

local iv c.logsharevz07#(i2016.year i2017.year i2018.year i2019.year i2020.year)

local ivp c.logsharevz07#(i2009.year i2010.year i2011.year i2012.year i2013.year i2014.year)

/*
egen  maxvzshare = max(sharevz), by( nombprov )
drop if maxvzshare < 0.004
*/


*------------------------ REGRESSIONS

* first-stage

forv i = 0/2 {

if `i'~=2 {
local sex "if female==`i'"
}

else {
local sex "female"
}

reghdfe share_combined_std `iv' `indvchar' `work' `sex', cluster(province_id) absorb(province_id i.month##i.year c.time#i.province_id)
testparm `iv', equal

foreach y of local outemp {

		if "`y'" == "employment_status" { 
		local ra replace 
		}
	
		else { 
		local ra append 
		}

	 if  ~inlist("`y'", "employment_status", "lnwage1", "lnhhinc", "lnhhexp", "formalemploy", "occ1") { 

		if `i'==2 {

	 	reghdfe `y' share_combined_std `indvchar' `work' `sex', cluster(province_id) absorb(province_id i.month##i.year) 
			outreg2 using "$analysis/part2_1`i'GT.xls", dec(3) nocons `ra' label ///
				addtext(Model, OLS, Region FE, Yes, Year FE, Yes, District FE, Yes, DistrictXTime trends, No)

		reghdfe `y' share_combined_std `indvchar' `work'  `sex', cluster(province_id) absorb(province_id c.time#i.province_id i.month##i.year)
			outreg2 using "$analysis/part2_2`i'GT.xls", dec(3) nocons `ra' label ///
		                addtext(Model, OLS, Region FE, Yes, Year FE, Yes, District FE, Yes, DistrictXTime trends, Yes) 
		}

		ivreghdfe `y' (share_combined_std=`iv') `indvchar' `work'  `sex', cluster(province_id) absorb(i.month##i.year province_id c.time#i.province_id)
			outreg2 using "$analysis/part2_3`i'GT.xls", dec(3) nocons `ra' label ///
				addtext(Model, IV, Region FE, Yes, Year FE, Yes, District FE, Yes, DistrictXTime trends, Yes) 
		}

	if  inlist("`y'", "employment_status", "lnwage1", "lnhhinc", "lnhhexp", "formalemploy", "occ1") {

		if `i'==2 {

		reghdfe `y' share_combined_std `indvchar' `sex', cluster(province_id) absorb(province_id i.month##i.year) 
			outreg2 using "$analysis/part2_1`i'GT.xls", dec(3) nocons `ra' label ///
				addtext(Model, OLS, Region FE, Yes, Year FE, Yes, District FE, Yes, DistrictXTime trends, No)

		reghdfe `y' share_combined_std `indvchar' `sex', cluster(province_id) absorb(province_id c.time#i.province_id i.month##i.year)
			outreg2 using "$analysis/part2_2`i'GT.xls", dec(3) nocons `ra' label ///
		                addtext(Model, OLS, Region FE, Yes, Year FE, Yes, District FE, Yes, DistrictXTime trends, Yes) 
		}
		
		ivreghdfe `y' (share_combined_std=`iv') `indvchar' `sex', cluster(province_id) absorb(i.month##i.year province_id c.time#i.province_id)
			outreg2 using "$analysis/part2_3`i'GT.xls", dec(3) nocons `ra' label ///
				addtext(Model, IV, Region FE, Yes, Year FE, Yes, District FE, Yes, DistrictXTime trends, Yes) 

		}

}
}

log close



